package com.codelab.statemachine.task;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Component
public class TaskExecuteHolder implements ApplicationContextAware {

    private Map<String,TaskExecute> holder = new HashMap<>();

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Collection<TaskExecute> tasks =  applicationContext.getBeansOfType(TaskExecute.class).values();
        tasks.stream().forEach(t -> holder.put(t.getTaskCode(),t));
    }

    public TaskExecute getBean(String taskCode){
        return holder.get(taskCode);
    }
}
